package com.android.corestat.database;

import java.util.ArrayList;
import java.util.concurrent.locks.ReentrantLock;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.android.corestat.utils.CSLog;

public class PackageDatabase {
	private static final String TAG = "cs-database";
	public static final String TABLE_NAME = "statisticTB";
	public static final String COL_SEQUENCE = "sequence";
	public static final String COL_STATUS = "status";
	public static final String COL_CONTENT = "content";

	private DBOpenHelper mDbOpenHelper;
	private ReentrantLock mLock;

	private static PackageDatabase mInstance;

	private PackageDatabase(Context context) {
		mDbOpenHelper = DBOpenHelper.instance(context);
		mLock = mDbOpenHelper.getLock();
	}

	public static PackageDatabase newInstance(Context context) {
		if (mInstance == null) {
			mInstance = new PackageDatabase(context);
		}
		return mInstance;
	}

	public boolean exist(int index) {
		if (index > 5 || index < 1) {
			throw new IllegalStateException("pkgseq must be in range [1, 5]");
		}

		SQLiteDatabase db = null;
		Cursor c = null;
		try {
			mLock.lock();
			db = mDbOpenHelper.getReadableDatabase();

			String[] columns = new String[] { COL_SEQUENCE };
			String selection = COL_SEQUENCE + "=?";
			String[] selectionArgs = new String[] { String.valueOf(index) };

			c = db.query(TABLE_NAME, columns, selection, selectionArgs, null, null, null);

            return c.moveToFirst();
        } catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (c != null) {
				c.close();
			}
			if (db != null) {
				db.close();
			}
			mLock.unlock();
		}
		return false;
	}

	public boolean append(int index, String content) {
		if (index > 5 || index < 1) {
			throw new IllegalStateException("pkgseq must be in range [1, 5] ");
		}
		if (content == null) {
			return false;
		}
		SQLiteDatabase db = null;
		try {
			mLock.lock();
			db = mDbOpenHelper.getWritableDatabase();

			ContentValues values = new ContentValues();
			values.put(COL_SEQUENCE, index);
			values.put(COL_STATUS, 0);
			values.put(COL_CONTENT, content);
			CSLog.i(TAG, "append index:" + index);
			long ret = db.insert(TABLE_NAME, null, values);
			return ret != -1;

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (db != null) {
				db.close();
			}
			mLock.unlock();
		}
		return false;
	}

	public boolean setSent(int index) {
		if (index > 5 || index < 1) {
			throw new IllegalStateException("pkgseq must be in range [1, 5] ");
		}

		SQLiteDatabase db = null;
		try {
			mLock.lock();
			db = mDbOpenHelper.getWritableDatabase();

			ContentValues values = new ContentValues();
			values.put(COL_STATUS, 1);
			String whereClause = COL_SEQUENCE + "=?";
			String[] whereArgs = new String[] { String.valueOf(index) };
			CSLog.i(TAG, "setSent : " + index);
			long ret = db.update(TABLE_NAME, values, whereClause, whereArgs);
			return ret != 0;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (db != null) {
				db.close();
			}
			mLock.unlock();
		}
		return false;
	}

	public ArrayList<String> getUnsents() {
		SQLiteDatabase db = null;
		Cursor cur = null;
		try {
			mLock.lock();
			db = mDbOpenHelper.getReadableDatabase();

			String selection = COL_STATUS + "=?";
			String[] selectionArgs = new String[] { String.valueOf(0) };

			cur = db.query(TABLE_NAME, null, selection, selectionArgs, null, null, null);
			ArrayList<String> ret = new ArrayList<String>();
			for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
				int contentColumn = cur.getColumnIndex(COL_CONTENT);
				String content = cur.getString(contentColumn);
				int seq = cur.getInt(cur.getColumnIndex(COL_SEQUENCE));
				CSLog.i(TAG, "get unsend:" + seq);
				ret.add(content);
			}
			return ret;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (cur != null) {
				cur.close();
			}
			if (db != null) {
				db.close();
			}
			mLock.unlock();
		}
		return null;
	}

	public ArrayList<Integer> getUnsentsSeq() {
		SQLiteDatabase db = null;
		Cursor cur = null;
		try {
			mLock.lock();
			db = mDbOpenHelper.getReadableDatabase();

			String[] columns = new String[] { COL_SEQUENCE };
			String selection = COL_STATUS + "=?";
			String[] selectionArgs = new String[] { String.valueOf(0) };

			cur = db.query(TABLE_NAME, columns, selection, selectionArgs, null, null, null);
			ArrayList<Integer> ret = new ArrayList<Integer>();
			for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
				int contentColumn = cur.getColumnIndex(COL_SEQUENCE);
				int seq = cur.getInt(contentColumn);
				ret.add(seq);
			}
			return ret;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (cur != null) {
				cur.close();
			}
			if (db != null) {
				db.close();
			}
			mLock.unlock();
		}
		return null;
	}

	/**
	 * �Ƿ��͹�10�Ű�
	 * 
	 * @return
	 */
	public boolean isSendTenth() {
		SQLiteDatabase db = null;
		Cursor cur = null;
		try {
			mLock.lock();
			db = mDbOpenHelper.getReadableDatabase();
			String[] columns = new String[] { COL_SEQUENCE, COL_STATUS };
			StringBuilder where = new StringBuilder();
			where.append(COL_SEQUENCE).append("=").append(10).append(" AND ").append(COL_STATUS)
					.append("=").append(1);
			cur = db.query(TABLE_NAME, columns, where.toString(), null, null, null, null);
			if (cur.moveToFirst()) {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (cur != null) {
				cur.close();
			}
			if (db != null) {
				db.close();
			}
			mLock.unlock();
		}
		return false;
	}
}
